local Camera = require"library/camera";
local Game2048 = require "game/game2048"

local player_width = (Game.CHECKERBOARD_GRID_SIZE * Game.CHECKERBOARD_WIDTH) + (Game.CHECKERBOARD_GRID_OFFSET * (Game.CHECKERBOARD_WIDTH + 1));
local player_height = Game.FONT_HEIGHT + Game.CHECKERBOARD_GRID_OFFSET + player_width 
local player_offset = 30

local game = class("GameScene",require"game/scene"()) {
    player1 = nil;
    player2 = nil;
    camera = nil;
}

function game:_init()
    self.name = "Game";

    local player1_x = 0;
    local player1_y = 0;
    self.player1 = Game2048(player1_x,player1_y,Game.Network.name);
    self.player2 = Game2048(player1_x + player_width + player_offset,player1_y);


    self.camera = Camera();
    self.camera:zoomTo(0.7)
    self.camera:lookAt(self.player2.x - player_offset / 2,player1_y + player_height / 4)

    Game.Network:addSigbalConnect("QuitRoom",self,self._QuitRoom)
    Game.Network:addSigbalConnect("KeyInput",self,self._KeyInput)
end

function game:_QuitRoom(pack)
    self.player1:restart()
    self.player1.label = "2048"
    if self.player2 then
        self.player2:restart()
        self.player2.label = "2048"
    end
    Game:switchScene("Room")
end

function game:_KeyInput(pack)
    local player_port = pack.player_port;
    local key = pack.key;
    local game_scene = Game.scene;
    local player = game_scene.player1;
    if player_port == Game.PLAYER_PORT_B then
        player = game_scene.player2;
    end

    player:input(key)
end

function game:_enter()
    love.window.setMode(800,600);
end

function game:_keypressed(key)
    Game.Network:requestKeyInput(key)
end

function game:_update(dt)

end

function game:_draw()
    self.camera:attach()
        self.player1:draw()
        self.player2:draw()
    self.camera:detach()
end

return game;